package com.isoft.hospital.service.Impl;

import com.isoft.hospital.common.MedicineData;
import com.isoft.hospital.common.ResponseData;
import com.isoft.hospital.dao.MedicineDao;
import com.isoft.hospital.entity.Medicine;
import com.isoft.hospital.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MedicineServiceImpl implements MedicineService {
    @Autowired
    private MedicineDao dao;
    @Override
    public ResponseData insertMedicine(Medicine medicine) {
        ResponseData rd=new ResponseData();
        int i=dao.insertMedicine(medicine);
        if(i>0){
            rd.setCode(0);
            rd.setMsg("添加成功");

        }else{
            rd.setCode(1);
            rd.setMsg("添加失败");
        }
        return rd;

    }

    @Override
    public ResponseData findAllMedicine() {
        ResponseData rd=new ResponseData();
        List<Medicine> list=new ArrayList<>();
        list=dao.findAllMedicine();
        if(list!=null){
            rd.setCode(0);
            rd.setMsg("查询成功");
            rd.setData(list);

        }else{
            rd.setCode(1);
            rd.setMsg("查询失败");
        }
        return rd;
    }

    @Override
    public ResponseData findByMid(int m_id) {
        ResponseData rd=new ResponseData();
        Medicine medicine=new Medicine();
        medicine=dao.findByMid(m_id);
        if(medicine!=null){
            rd.setCode(0);
            rd.setMsg("查询成功");
            List<Medicine> list=new ArrayList<>();
            list.add(medicine);
            rd.setData(list);

        }else{
            rd.setCode(1);
            rd.setMsg("查询失败");

        }
        return rd;
    }

    @Override
    public ResponseData updateMedicine(Medicine medicine) {
        ResponseData rd=new ResponseData();
        int i=dao.updateMedicine(medicine);

        if(i>0){

            rd.setCode(0);
            rd.setMsg("更新成功");


        }else{
            rd.setCode(1);
            rd.setMsg("更新失败");
        }
        return rd;
    }

    @Override
    public ResponseData deleteMedicine(int m_id) {
        ResponseData rd=new ResponseData();
        int i=dao.deleteMedicine(m_id);

        if(i>0){

            rd.setCode(0);
            rd.setMsg("删除成功");


        }else{
            rd.setCode(1);
            rd.setMsg("删除失败");
        }
        return rd;

    }

    @Override
    public MedicineData queryMedicine() {
       MedicineData md=new MedicineData();
        List<Medicine> list=dao.queryMedicine();
        List<String> mnames=new ArrayList<>();
        List<String> stocks=new ArrayList<>();
        for(Medicine medicine:list){
            mnames.add(medicine.getM_name());
            stocks.add(medicine.getStock());
            md.setMnames(mnames);
            md.setStocks(stocks);
        }
        return md;


    }
}
